package com.tchintech.pay.model.base;

import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import com.tchintech.pay.commod.BizInterface;
import com.tchintech.pay.listeners.OnNetRequestListener;
import com.tchintech.pay.model.entity.Memberinfo;
import com.tchintech.pay.model.entity.ResultBean;
import com.tchintech.pay.server.RetrofitService;
import com.tchintech.pay.utils.MD5;
import com.tchintech.pay.utils.StringUtil;

import org.json.JSONObject;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * cn.woogo.micp.model.base
 * <p>
 * <p>
 * by John
 * Created  on 2016/5/19 0019.
 */
public class
CommitDataImpl implements CommitData {

    @Override
    public void commitData(String path, Object o, Memberinfo memberinfo, final OnNetRequestListener listener) {

        String json = StringUtil.object2json(o);

        String memberinfoJson = StringUtil.object2json(memberinfo);
        try {

            json = URLEncoder.encode(json, "UTF-8");
            memberinfoJson = URLEncoder.encode(memberinfoJson, "UTF-8");

            RetrofitService retrofitService = new RetrofitService().getInstance();
            //使用RxJava响应Retrofit
            Observable<String> observable = retrofitService.
                    createCommitApi().commitAnyThing(path, json, memberinfoJson);

            observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnSubscribe(new Action0() {
                        @Override
                        public void call() {
                            listener.onStart();
                        }
                    })
                    .subscribe(new Subscriber<String>() {
                        @Override
                        public void onCompleted() {
                            //仅成功后会回调
                            System.out.println("onCompleted");
                            listener.onFinish();
                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                            System.out.println("onError");
                            System.out.println("异常");
                            listener.onFailure(e);
                            listener.onFinish();
                        }

                        /**
                         * Provides the Observer with a new item to observe.
                         * <p/>
                         * The {@link Observable} may call this method 0 or more times.
                         * <p/>
                         * The {@code Observable} will not call this method again after it calls either {@link #onCompleted} or
                         * {@link #onError}.
                         *
                         * @param resultBean the item emitted by the Observable
                         */
                        @Override
                        public void onNext(String resultBean) {
                            try {
                                System.out.println("成功");
                                String reString = URLDecoder.decode(resultBean, "UTF-8");
                                Gson gson = new Gson();
                                ResultBean rb = gson.fromJson(reString, ResultBean.class);
                                //    System.out.println(rb);
                                listener.onSuccess(rb);
                            } catch (UnsupportedEncodingException e)  {
                                e.printStackTrace();
                            }
                        }

                    });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void commitBigData(String path, Object o, Memberinfo memberinfo, final OnNetRequestListener listener2) {


        String json = StringUtil.object2json(o);

        String memberinfoJson = StringUtil.object2json(memberinfo);
        try {

            json = URLEncoder.encode(json, "UTF-8");
            memberinfoJson = URLEncoder.encode(memberinfoJson, "UTF-8");

            RetrofitService retrofitService = new RetrofitService().getInstance();
            //使用RxJava响应Retrofit
            Observable<String> observable = retrofitService.
                    createCommitApi().commitBigThing(path, json, memberinfoJson);

            observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnSubscribe(new Action0() {
                        @Override
                        public void call() {
                            //listener.onStart();
                        }
                    })
                    .subscribe(new Subscriber<String>() {
                        @Override
                        public void onCompleted() {
                            //仅成功后会回调
                            System.out.println("onCompleted");
                            listener2.onFinish();
                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                            System.out.println("onError");
                            listener2.onFailure(e);
                            listener2.onFinish();
                        }

                        /**
                         * Provides the Observer with a new item to observe.
                         * <p/>
                         * The {@link Observable} may call this method 0 or more times.
                         * <p/>
                         * The {@code Observable} will not call this method again after it calls either {@link #onCompleted} or
                         * {@link #onError}.
                         *
                         * @param resultBean the item emitted by the Observable
                         */
                        @Override
                        public void onNext(String resultBean) {
                            try {
                                String reString = URLDecoder.decode(resultBean, "UTF-8");
                                Gson gson = new Gson();
                                ResultBean rb = gson.fromJson(reString, ResultBean.class);
                                System.out.println(rb);
                                listener2.onSuccess(rb);
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                        }

                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void conlogin(String path, Object o, final OnNetRequestListener listener2) {
          String json = StringUtil.object2json(o);
        //   JSONObject jsonObject= (JSONObject) o;
        try {
            //json = URLEncoder.encode(json, "UTF-8");
            RetrofitService retrofitService = new RetrofitService().getInstance();
            //使用RxJava响应Retrofit

            System.out.println("数据开始请求等待结果"+ BizInterface.API+path+"数据"+json+"加密数据"+MD5.getMD5Str(json));
            String paths="Merchant/login";
            Observable<String> observable = retrofitService.createCommitApi().commitLogin(json);

            System.out.println("请求开始"+observable);

            observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnSubscribe(new Action0() {
                        @Override
                        public void call() {
                             listener2.onStart();
                        }
                    })
                    .subscribe(new Subscriber<String>() {
                        @Override
                        public void onCompleted() {
                            //仅成功后会回调
                            System.out.println("onCompleted");
                            listener2.onFinish();
                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                            System.out.println("onError");
                            System.out.println("异常"+e.getMessage());
                            listener2.onFailure(e);
                            listener2.onFinish();
                        }

                        /**
                         * Provides the Observer with a new item to observe.
                         * <p/>
                         * The {@link Observable} may call this method 0 or more times.
                         * <p/>
                         * The {@code Observable} will not call this method again after it calls either {@link #onCompleted} or
                         * {@link #onError}.
                         *
                         * @param resultBean the item emitted by the Observable
                         */
                        @Override
                        public void onNext(String resultBean) {
                            try {
                                System.out.println("返回值："+resultBean);
                                String reString = URLDecoder.decode("", "UTF-8");
                                Gson gson = new Gson();
                                ResultBean rb = gson.fromJson(reString, ResultBean.class);
                                listener2.onSuccess(rb);
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                        }

                    });
        } catch (Exception e) {
            System.out.println("错误"
            );
            e.printStackTrace();
        }

    }
}
